/**
 * @copyright (C) COPYRIGHT 2022 Fortiortech Shenzhen
 * @file      Protect.h
 * @author    Marcel He
 * @note      Last modify author is Marcel He
 * @since     2022-07-01
 * @date      2022-07-14
 * @brief     This file contains protection parameter used for Motor Control.
 */

/* Define to prevent recursive inclusion -------------------------------------------------------- */
#ifndef __PROTECT_H_
#define __PROTECT_H_

/*保护参数设置--------------------------------------------------------------------------------------------*/

/* HW current protection */
#define HW_OC_FO_MODE    (1)             ///< 硬件FO过流保护使能，适用于IPM有FO保护的场合
#define HW_OC_CMP_MODE   (2)             ///< 硬件CMP比较过流保护使能，适用于MOS管应用场合
#define HW_OC_FOCMP_MODE (3)             ///< 硬件CMP比较和FO过流保护都使能
#define HW_OC_DISABLE    (4)             ///< 硬件过流保护禁止，用于测试
#define HW_OC_MODE       (HW_OC_DISABLE) ///< 硬件过流保护实现方式

/* 硬件过流保护比较值来源 */
#define COMPARE_DAC_MODE (0)                ///< DAC设置硬件过流值
#define COMPARE_HW_MODE  (1)                ///< 硬件设置硬件过流值
#define COMPARE_MODE     (COMPARE_DAC_MODE) ///< 硬件过流值的来源
#define HWOCValue        (80.0)             ///< (A) DAC模式下的硬件过流值

/* 软件过流保护参数设置 */
#define SW_OC_CurrentVal I_Value(10.0) ///< 洗涤模式第一电流限定值

/* Faults processing Enable */

#define SWCurrentProtectEn (0) ///< 软件过流保护使能： 0,不使能；1，使能
#define VoltageProtectEn   (0) ///< 过欠压保护使能：   0,不使能；1，使能
#define StallProtectEn     (0) ///< 堵转失速保护使能： 0,不使能；1，使能
#define PhaseLossProtectEn (0) ///< 缺相保护使能：     0,不使能；1，使能
#define OverTPProtectEn    (0) ///< 过温保护使能：     0,不使能；1，使能
#define OffsetProtectEn    (0) ///< 偏置电压保护：     0,不使能；1，使能

/* 软件保护恢复使能 */
#define ProtectRecoveryEn (1)

/* 直流母线电压保护参数设置值 */
#define OVER_VOLTAGE_PROTECT  UDC_Value(32) ///< (V) 直流母线电压过压保护值
#define UNDER_VOLTAGE_PROTECT UDC_Value(18) ///< (V) 直流母线电压欠压保护值

#define OVER_VOLTAGE_RECOVER  UDC_Value(30)   ///< (V) 直流母线电压过压保护恢复值
#define UNDER_VOLTAGE_RECOVER UDC_Value(18.5) ///< (V) 直流母线电压欠压保护恢复值

/* 堵转保护参数设置值 */
#define STALL_MAX_SPEED S_Value(16000)
#define STALL_MIN_SPEED S_Value(200)

#define EsThresholdValue (500.0)
#define EsThresholdSpeed S_Value(300.0) ///< (RPM) 电机转速

/* 缺相保护参数设置值 */
#define PhaseLossCurrentValue I_Value(0.6) ///< (A) 缺相电流值

/* -----TSD过温保护----- */
#define TempProtValue    150 ///< （℃）保护温度
#define TempRecoverValue 125 ///< （℃）恢复温度

/* -----偏置电压保护----- */
#define GetCurrentOffsetValue _Q14(0.10) // (单位:100%)偏置电压保护误差范围，超过该范围保护

/* 故障显示 */
#define LED_ONTime       (100)
#define LED_OFFTime      (233)
#define LED_ONOFFTime    (LED_ONTime + LED_OFFTime)
#define LED_DelayTime    (200)
#define LED_IntervalTime (1000)

#define POSTErrNoSource 0
#define POSTErrPhaseA   0x01
#define POSTErrPhaseB   0x02
#define POSTErrPhaseC   0x04
#define POSTErrOffSetU  0x08
#define POSTErrOffSetV  0x10
#define POSTErrIpmNTC   0x20
#define POSTErrDCbus    0x40
#define POSTErrFO       0x80

/* Exported types ------------------------------------------------------------ */

typedef enum
{
    FaultNoSource       = 0,  ///< 无故障
    FaultHardOVCurrent  = 1,  ///< 硬件过流
    FaultSoftOVCurrent  = 2,  ///< 软件过流
    FaultOverVoltageDC  = 3,  ///< 过压
    FaultUnderVoltageDC = 4,  ///< 欠压
    FaultPhaseLost      = 5,  ///< 缺相
    FaultStall          = 6,  ///< 堵转
    FaultNtcOTErr       = 7,  ///< NTC过温
    FaultMotorOTErr     = 8,  ///< 电机过温
    FaultTSD            = 9,  ///< MCU内部过温
    FaultOffset         = 10, ///< 偏置电压保护
    FaultPOST           = 11, ///< FCT自检故障

} FaultStateType;

typedef struct
{
    uint8 OverCurACnt;
    uint8 OverCurBCnt;
    uint8 OverCurCCnt;

    uint8 HDOC_Times;
    uint8 HDOC_DectTimeCnt;

} FaultCurrentVarible;

typedef struct
{
    uint16 DetecCnt;

    uint16 WarningDetecCnt;

    uint8 WarningFlag;

} FaultTemperatureVarible;

typedef struct
{
    uint16 Lphasecnt;
    uint16 AOpencnt;
    uint16 BOpencnt;
    uint16 COpencnt;
    uint16 ABCOpenCnt;
    uint16 mcLossPHRecCount;
    uint16 DectDealyCnt;

} FaultPhaseLossVarible;

typedef struct
{
    uint16 OverVoltDetecCnt;
    uint16 UnderVoltDetecCnt;

    uint16 VoltRecoverCnt;

    uint16 BusVoltDetecCnt;

    uint16 DectDealyCnt;

    uint16 OverVoltageVal;
    uint16 UnderVoltageVal;

    uint16 OverACVoltageVal;
    uint16 UnderACVoltageVal;

} FaultVoltageVarible;

typedef struct
{
    uint16 Mode0DectCnt;
    uint8  JDectCnt;
    int16  SpeedErr;
    uint16 DeviSpeedCnt;

    uint16 EsDectCnt;
    uint16 SpeedDectCnt;
    uint16 SpeedMinCnt;
    int16  SpeedAcc;
    uint16 J_inverse;
    uint16 J_inverse_lpf;
    int16  OverSpeedVal;
    int16  UnderSpeedVal;
    int16  EsSpeedVal;

    uint16 DectDealyCnt;
    uint8  Type;

} FaultStallTypedef;

typedef struct
{
    uint16 HighLevelTime;
    uint16 LowLevelTime;
    uint32 MaxVoltage;
    uint8  Flag_Voltage;
} FaultACLossTypedef;

typedef struct
{
    uint8  AC_Times;
    uint16 AC_DelayTcnt;
    uint8  DC_Times;

    uint16 DC_DelayTcnt;

    uint16 OT_DelayTcnt;

    uint16 UartDelaycnt;

    uint8  SWOC_Times;
    uint16 SWOC_DelayTcnt;

    uint16 WT_DelayTcnt;

    uint16 LP_DelayTcnt;
    uint8  LP_Times;
    uint16 Stall_DealyTcnt;
    uint8  Stall_Times;

} FaultRecoverTypedef;

typedef struct
{
    //    FaultCurrentVarible         Current;
    FaultPhaseLossVarible   PhaseLoss;
    FaultVoltageVarible     Voltage;
    FaultStallTypedef       Stall;
    FaultTemperatureVarible Temperature;

} FaultVarible;

/* Exported variables ---------------------------------------------------------------------------*/

extern FaultStateType data mcFaultSource;
extern uint8 xdata         mcPOSTErrSource;
extern FaultVarible xdata  Fault;

extern FaultCurrentVarible idata mcCurVarible;

extern FaultRecoverTypedef xdata Restart;

/* Exported functions ---------------------------------------------------------------------------*/

extern void Fault_OverCurrent(FaultCurrentVarible *h_Cur);

extern void Fault_Stall(void);
extern void Fault_Temperature(void);
extern void Fault_Voltage(void);

extern void Fault_PhaseLoss(void);

extern void Fault_Detection(void);
extern void Fault_Communication(void);
extern void Fault_GetCurrentOffset(void);

extern void Fault_Recovery(void);

extern void Fault_Process(void);

#endif
